<template>
    <el-dialog :title="form_title" :visible.sync="isShow" width="40%">
        <div class="dialog">
            <el-button v-for="item in tableMenu" :type="item.type" size="small" :icon="item.icon" @click="item.click">
                {{ item.text }}
            </el-button>
        </div>
        <selfins ref="selfins" />
    </el-dialog>
</template>
<script>
import { requestApi } from '@/requset/api'
import { tableMenu1, tableMenu2 } from './formConfig'
import { deepClone } from '@/plugins/clone'
import selfins from './selfins.vue'
export default {
    data() {
        return {
            isShow: false,
            tableMenu: null,
            activeItem: null,
        }
    },
    components: {
        selfins,
    },
    methods: {
        doOpen(e) {
            let { mode } = e

            this.initMenu()
            if (mode == 1) {
                this.tableMenu = deepClone(tableMenu1)
            } else if (mode == 2) {
                tableMenu2[2].click = () => this.selfins(e)
                this.tableMenu = deepClone(tableMenu2)
            }
            this.activeItem = e
            this.isShow = true
        },
        sendMsg() {
            const topic = 'acq'
            let { number } = this.activeItem
            let message = JSON.stringify(this.activeItem)
            requestApi('post', 'wavecoll/send/directive', { message, number }).then((res) => {
                if (res.data.code == 0) {
                    console.log('Redis指令下发成功')
                } else this.$message.info('Redis指令下发失败')
            })
            this.$mqtt.publish(topic, message, (err) => {
                if (err) {
                    this.$message.info('下发指令失败')
                } else {
                    this.$message.success('下发指令成功')
                }
            })
        },
        updateMqtt() {
            this.$EventBus.$emit('undateMqtt')
            this.sendMsg()
        },
        initMenu() {
            tableMenu1[0].click = () => this.sendMsg()
            tableMenu1[1].click = () => this.$EventBus.$emit('undateMqtt')
            tableMenu2[0].click = () => this.sendMsg()
            tableMenu2[1].click = () => this.$EventBus.$emit('undateMqtt')
        },
        selfins(e) {
            this.$refs.selfins.doOpen(e)
        },
    },
    created() {
        // tableMenu[0].click = () => this.sendMsg()
        // tableMenu[1].click = () => this.$EventBus.$emit('undateMqtt')
        // this.tableMenu = deepClone(tableMenu)
    },
}
</script>

<style lang="less" scoped></style>
